home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / OldSrc / CH6 / SRC / HERMITE.FRM (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1996-03-29  |  9.5 KB  |  315 lines

  1. VERSION 4.00
  2. Begin VB.Form HermiteForm 
  3.    AutoRedraw      =   -1  'True
  4.    Caption         =   "Hermite Curve"
  5.    ClientHeight    =   5685
  6.    ClientLeft      =   1650
  7.    ClientTop       =   645
  8.    ClientWidth     =   4830
  9.    Height          =   6375
  10.    Left            =   1590
  11.    LinkTopic       =   "Form1"
  12.    ScaleHeight     =   379
  13.    ScaleMode       =   3  'Pixel
  14.    ScaleWidth      =   322
  15.    Top             =   15
  16.    Width           =   4950
  17.    Begin VB.CheckBox ControlCheck 
  18.       Caption         =   "Draw Control Points"
  19.       Height          =   255
  20.       Left            =   1440
  21.       TabIndex        =   12
  22.       Top             =   60
  23.       Value           =   1  'Checked
  24.       Width           =   1815
  25.    End
  26.    Begin VB.CommandButton CmdGo 
  27.       Caption         =   "Go"
  28.       Height          =   375
  29.       Left            =   4320
  30.       TabIndex        =   11
  31.       Top             =   0
  32.       Width           =   495
  33.    End
  34.    Begin VB.TextBox Vy2Text 
  35.       Height          =   285
  36.       Left            =   4200
  37.       TabIndex        =   9
  38.       Text            =   "500"
  39.       Top             =   480
  40.       Width           =   615
  41.    End
  42.    Begin VB.TextBox Vx2Text 
  43.       Height          =   285
  44.       Left            =   3120
  45.       TabIndex        =   7
  46.       Text            =   "-500"
  47.       Top             =   480
  48.       Width           =   615
  49.    End
  50.    Begin VB.TextBox Vy1Text 
  51.       Height          =   285
  52.       Left            =   1440
  53.       TabIndex        =   5
  54.       Text            =   "-500"
  55.       Top             =   480
  56.       Width           =   615
  57.    End
  58.    Begin VB.TextBox Vx1Text 
  59.       Height          =   285
  60.       Left            =   360
  61.       TabIndex        =   3
  62.       Text            =   "-500"
  63.       Top             =   480
  64.       Width           =   615
  65.    End
  66.    Begin VB.TextBox DtText 
  67.       Height          =   285
  68.       Left            =   240
  69.       TabIndex        =   2
  70.       Text            =   "0.01"
  71.       Top             =   45
  72.       Width           =   615
  73.    End
  74.    Begin VB.PictureBox Canvas 
  75.       AutoRedraw      =   -1  'True
  76.       Height          =   4815
  77.       Left            =   0
  78.       ScaleHeight     =   317
  79.       ScaleMode       =   3  'Pixel
  80.       ScaleWidth      =   317
  81.       TabIndex        =   0
  82.       Top             =   840
  83.       Width           =   4815
  84.    End
  85.    Begin VB.Label Label1 
  86.       Caption         =   "Vy2"
  87.       Height          =   255
  88.       Index           =   4
  89.       Left            =   3840
  90.       TabIndex        =   10
  91.       Top             =   510
  92.       Width           =   375
  93.    End
  94.    Begin VB.Label Label1 
  95.       Caption         =   "Vx2"
  96.       Height          =   255
  97.       Index           =   3
  98.       Left            =   2760
  99.       TabIndex        =   8
  100.       Top             =   510
  101.       Width           =   375
  102.    End
  103.    Begin VB.Label Label1 
  104.       Caption         =   "Vy1"
  105.       Height          =   255
  106.       Index           =   2
  107.       Left            =   1080
  108.       TabIndex        =   6
  109.       Top             =   510
  110.       Width           =   375
  111.    End
  112.    Begin VB.Label Label1 
  113.       Caption         =   "Vx1"
  114.       Height          =   255
  115.       Index           =   0
  116.       Left            =   0
  117.       TabIndex        =   4
  118.       Top             =   510
  119.       Width           =   375
  120.    End
  121.    Begin VB.Label Label1 
  122.       Caption         =   "dt"
  123.       Height          =   255
  124.       Index           =   1
  125.       Left            =   0
  126.       TabIndex        =   1
  127.       Top             =   60
  128.       Width           =   255
  129.    End
  130.    Begin VB.Menu mnuFile 
  131.       Caption         =   "&File"
  132.       Begin VB.Menu mnuFileExit 
  133.          Caption         =   "E&xit"
  134.       End
  135.    End
  136. Attribute VB_Name = "HermiteForm"
  137. Attribute VB_Creatable = False
  138. Attribute VB_Exposed = False
  139. Option Explicit
  140. Const PI = 3.14159
  141. Const GAP = 3
  142. ' The endpoints.
  143. Const NumPts = 2
  144. Dim PtX(1 To NumPts) As Single
  145. Dim PtY(1 To NumPts) As Single
  146. ' The index of the point being dragged.
  147. Dim Dragging As Integer
  148. Dim OldMode As Integer
  149. ' The hermite curve parameters.
  150. Dim Ax As Single
  151. Dim Bx As Single
  152. Dim Cx As Single
  153. Dim Dx As Single
  154. Dim Ay As Single
  155. Dim By As Single
  156. Dim Cy As Single
  157. Dim Dy As Single
  158. ' ************************************************
  159. ' Draw the curve on the indicated picture box.
  160. ' ************************************************
  161. Sub DrawCurve(pic As PictureBox, start_t As Single, stop_t As Single, dt As Single)
  162. Dim x1 As Single
  163. Dim y1 As Single
  164. Dim t As Single
  165.     x1 = X(start_t)
  166.     y1 = Y(start_t)
  167.     pic.Cls
  168.     pic.CurrentX = x1
  169.     pic.CurrentY = y1
  170.     t = start_t + dt
  171.     Do While t < stop_t
  172.         x1 = X(t)
  173.         y1 = Y(t)
  174.         pic.Line -(x1, y1)
  175.         t = t + dt
  176.     Loop
  177.     x1 = X(stop_t)
  178.     y1 = Y(stop_t)
  179.     pic.Line -(x1, y1)
  180. End Sub
  181. ' ************************************************
  182. ' Compute the Hermite curve parameters.
  183. ' ************************************************
  184. Sub GetHermiteValues(ex1 As Single, ey1 As Single, ex2 As Single, ey2 As Single, vx1 As Single, vy1 As Single, vx2 As Single, vy2 As Single, Ax As Single, Bx As Single, Cx As Single, Dx As Single, Ay As Single, By As Single, Cy As Single, Dy As Single)
  185.     Ax = vx2 + vx1 - 2 * ex2 + 2 * ex1
  186.     Bx = 3 * ex2 - 2 * vx1 - 3 * ex1 - vx2
  187.     Cx = vx1
  188.     Dx = ex1
  189.     Ay = vy2 + vy1 - 2 * ey2 + 2 * ey1
  190.     By = 3 * ey2 - 2 * vy1 - 3 * ey1 - vy2
  191.     Cy = vy1
  192.     Dy = ey1
  193. End Sub
  194. ' ************************************************
  195. ' The parametric function Y(t).
  196. ' ************************************************
  197. Function Y(t As Single) As Single
  198.     Y = Ay * t ^ 3 + By * t * t + Cy * t + Dy
  199. End Function
  200. ' ************************************************
  201. ' The parametric function X(t).
  202. ' ************************************************
  203. Function X(t As Single) As Single
  204.     X = Ax * t ^ 3 + Bx * t * t + Cx * t + Dx
  205. End Function
  206. ' ************************************************
  207. ' Prepare to draw the Hermite curve.
  208. ' ************************************************
  209. Private Sub DrawHermite()
  210. Const DOTTED = 2
  211. Dim vx1 As Single
  212. Dim vy1 As Single
  213. Dim vx2 As Single
  214. Dim vy2 As Single
  215. Dim dt As Single
  216. Dim i As Integer
  217.     ' Compute the curve parameters.
  218.     vx1 = CSng(Vx1Text.Text)
  219.     vy1 = CSng(Vy1Text.Text)
  220.     vx2 = CSng(Vx2Text.Text)
  221.     vy2 = CSng(Vy2Text.Text)
  222.     GetHermiteValues _
  223.         PtX(1), PtY(1), PtX(2), PtY(2), _
  224.         vx1, vy1, vx2, vy2, _
  225.         Ax, Bx, Cx, Dx, Ay, By, Cy, Dy
  226.     ' Draw the curve.
  227.     dt = CSng(DtText.Text)
  228.     DrawCurve Canvas, 0, 1, dt
  229.     If ControlCheck.Value = vbChecked Then
  230.         ' Draw the control points.
  231.         For i = 1 To NumPts
  232.             Canvas.Line _
  233.                 (PtX(i) - GAP, PtY(i) - GAP)- _
  234.                 Step(2 * GAP, 2 * GAP), , BF
  235.         Next i
  236.         
  237.         ' Draw the tangents.
  238.         OldMode = Canvas.DrawStyle
  239.         Canvas.DrawStyle = DOTTED
  240.         Canvas.Line (PtX(1), PtY(1))- _
  241.             (PtX(1) + vx1 / 5, PtY(1) + vy1 / 5)
  242.         Canvas.Line (PtX(2), PtY(2))- _
  243.             (PtX(2) + vx2 / 5, PtY(2) + vy2 / 5)
  244.         Canvas.DrawStyle = OldMode
  245.     End If
  246. End Sub
  247. ' ************************************************
  248. ' Select a point and start dragging it.
  249. ' ************************************************
  250. Private Sub Canvas_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
  251. Dim i As Integer
  252.     ' Find a close point.
  253.     For i = 1 To NumPts
  254.         If Abs(PtX(i) - X) <= GAP And _
  255.            Abs(PtY(i) - Y) <= GAP Then Exit For
  256.     Next i
  257.     If i > NumPts Then Exit Sub
  258.     Dragging = i
  259.     OldMode = Canvas.DrawMode
  260.     Canvas.DrawMode = vbInvert
  261.     PtX(Dragging) = X
  262.     PtY(Dragging) = Y
  263.     Canvas.Line _
  264.         (PtX(Dragging) - GAP, PtY(Dragging) - GAP)- _
  265.         Step(2 * GAP, 2 * GAP), , BF
  266. End Sub
  267. ' ************************************************
  268. ' Continue dragging a point.
  269. ' ************************************************
  270. Private Sub Canvas_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
  271.     If Dragging < 1 Then Exit Sub
  272.     Canvas.Line _
  273.         (PtX(Dragging) - GAP, PtY(Dragging) - GAP)- _
  274.         Step(2 * GAP, 2 * GAP), , BF
  275.     PtX(Dragging) = X
  276.     PtY(Dragging) = Y
  277.     Canvas.Line _
  278.         (PtX(Dragging) - GAP, PtY(Dragging) - GAP)- _
  279.         Step(2 * GAP, 2 * GAP), , BF
  280. End Sub
  281. ' ************************************************
  282. ' Finish the drag and redraw the curve.
  283. ' ************************************************
  284. Private Sub Canvas_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
  285.     If Dragging < 1 Then Exit Sub
  286.     Canvas.DrawMode = OldMode
  287.     PtX(Dragging) = X
  288.     PtY(Dragging) = Y
  289.     Dragging = 0
  290.     DrawHermite
  291. End Sub
  292. Private Sub CmdGo_Click()
  293.     DrawHermite
  294. End Sub
  295. Private Sub ControlCheck_Click()
  296.     DrawHermite
  297. End Sub
  298. Private Sub Form_Load()
  299.     PtX(1) = 0.5 * Canvas.ScaleWidth
  300.     PtX(2) = 0.8 * Canvas.ScaleWidth
  301.     PtY(1) = 0.7 * Canvas.ScaleHeight
  302.     PtY(2) = 0.5 * Canvas.ScaleHeight
  303. End Sub
  304. ' ************************************************
  305. ' Make the canvas as big as possible.
  306. ' ************************************************
  307. Private Sub Form_Resize()
  308.     Canvas.Move 0, Canvas.Top, _
  309.         ScaleWidth, ScaleHeight - Canvas.Top
  310.     DrawHermite
  311. End Sub
  312. Private Sub mnuFileExit_Click()
  313.     Unload Me
  314. End Sub
  315.